import { createRouter, createWebHistory, type RouteRecordRaw } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import Layout from '@/views/Layout/Layout.vue'
import Mobile from '@/views/Mobile/Mobile.vue'
export type RouteItem = RouteRecordRaw & {
  icon?: string
  code?: number
  children?: RouteRecordRaw[]
}
export const routes: RouteItem[] = [
  {
    path: '/index',
    meta: {
      code: 1
    },
    name: 'home-index',
    component: HomeView
  },
  {
    path: '/dashboard',
    name: 'dashboard',
    meta: {
      code: 1
    },
    component: () => import('@/views/Redash/Redash.vue')
  },
  {
    path: '/user-list',
    meta: {
      code: 2
    },
    icon: 'UserFilled',
    name: '用户管理',
    component: () => import('@/views/UserManage/UserList.vue')
  },
  {
    path: '/user-detail',
    name: '用户详情',
    meta: {
      code: 2,
      hide: true
    },
    component: () => import('@/views/UserManage/UserDetail.vue')
  },
  {
    path: '/role-list',
    meta: {
      code: 3
    },
    icon: 'UserFilled',
    name: '角色管理',
    component: () => import('@/views/RoleManage/RoleList.vue')
  },
  {
    path: '/auto-config',
    meta: {
      code: 4
    },
    icon: 'UserFilled',
    name: '权限管理',
    component: () => import('@/views/AuthManage/AuthList.vue')
  },
  {
    path: '/about',
    meta: {
      code: 5
    },
    icon: 'setting',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (About.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import('../views/AboutView.vue')
  }
]
export const RouteData: RouteItem[] = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login/Login.vue'),
  },
  {
    path: '/redash',
    name: 'Redash',
    component: () => import('@/views/Redash/Redash.vue')
  },
  {
    path: '/404',
    name: 'NotFound',
    component: () => import('@/views/404.vue')
  },
  {
    path: '/mobile',
    name: 'mobile',
    component: Mobile
  },
  {
    path: '/',
    name: 'home',
    icon: 'FullScreen',
    component: Layout,
    redirect: '/index',
    children: routes
  }
]
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: RouteData
})
const routeWhiteList = ['/login','/404','/redash','/mobile']
router.beforeEach((to, from, next) => {
  console.log(to)
  if(routeWhiteList.includes(to.path)) {
    next()
    return
  }
  const authCodes = localStorage.getItem('authCodes')?.split(',')
  console.log(authCodes, to.meta?.code)
  const isAuth = authCodes?.includes(to.meta?.code+'')
  console.log(isAuth)
  if(isAuth) {next()}
  else {
    next('/404')
  }
})
export default router
